package com.rice.riceaiagent.app;

import com.rice.riceaiagent.advisor.MyLoggerAdvisor;
import com.rice.riceaiagent.advisor.MyReReadingAdvisor;
import com.rice.riceaiagent.memory.MyFileChatMemory;
import jakarta.annotation.PostConstruct;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.MessageChatMemoryAdvisor;
import org.springframework.ai.chat.client.advisor.QuestionAnswerAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.model.ChatModel;
import org.springframework.ai.vectorstore.VectorStore;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY;
import static org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor.CHAT_MEMORY_RETRIEVE_SIZE_KEY;

/**
 * 简历辅导应用
 * @author ricejson
 */
@Component
public class ResumeApp {
    private ChatClient chatClient;

    @Autowired
    public ResumeApp(ChatModel dashscopeChatModel, @Value("classpath:/prompts/system.st") Resource systemPrompt) throws IOException {
        // 修改成文件存储记忆
        ChatMemory chatMemory = new MyFileChatMemory(System.getProperty("user.dir") + "/tmp/");
        this.chatClient = ChatClient.builder(dashscopeChatModel)
                .defaultSystem(systemPrompt) // 系统预设提示词
                .defaultAdvisors(new MessageChatMemoryAdvisor(chatMemory), // 支持多轮对话
                        new MyLoggerAdvisor(), // 自定义日志Advisor
                        new MyReReadingAdvisor()) // 自定义ReReading Advisor
                .build();
    }

    public String doChat(String userMsg, String chatId) {
        // 与AI对话
        return this.chatClient.prompt()
                .user(userMsg)
                .advisors((spec) -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .content();
    }

    // 定义简历报告类
    record ResumeReport(String title, List<String> suggestions) {}

    public ResumeReport doChatWithReport(String userPrompt, String chatId) {
        // 返回简历报告
        return chatClient.prompt()
                .user(userPrompt + "请生成对应的简历辅导建议报告，要求包含{title}标题字段和{suggestions}建议列表字段")
                .advisors((spec) -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                .call()
                .entity(ResumeReport.class);
    }

    @jakarta.annotation.Resource
    private VectorStore resumeAppVectorStore;

    public String doChatWithRag(String userPrompt, String chatId) {
        return chatClient.prompt()
                .user(userPrompt)
                .advisors((spec) -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                // 开启日志拦截器
                .advisors(new MyLoggerAdvisor())
                // 开启知识问答RAG拦截器
                .advisors(new QuestionAnswerAdvisor(resumeAppVectorStore))
                .call()
                .content();
    }

    @jakarta.annotation.Resource
    private Advisor resumeAppCloudAdvisor;

    public String doChatWithCloudRag(String userPrompt, String chatId) {
        return chatClient.prompt()
                .user(userPrompt)
                .advisors((spec) -> spec.param(CHAT_MEMORY_CONVERSATION_ID_KEY, chatId)
                        .param(CHAT_MEMORY_RETRIEVE_SIZE_KEY, 10))
                // 开启日志拦截器
                .advisors(new MyLoggerAdvisor())
                // 开启云知识问答RAG拦截器
                .advisors(resumeAppCloudAdvisor)
                .call()
                .content();
    }

}
